/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.SimpleKeyValueBean;
import com.naizhuai.ui.entity.mainbusiness.ActionShowMainItemBean;
import com.naizhuai.util.DeviceUtils;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.NaizhuAILog;

import java.io.File;
import java.util.List;

/**
 * 待揭榜adapter
 */
public class ActionMainWaitAdapterHolder extends RecyclerView.Adapter {

    private OnItemClickListener mItemClickListener;
    private Context mContext;
    private List<ActionShowMainItemBean> actionItemBeans;


    public ActionMainWaitAdapterHolder(Context context, List<ActionShowMainItemBean> actionItemBean) {
        this.mContext = context;
        this.actionItemBeans = actionItemBean;
    }

    @Override
    public int getItemCount() {
        return actionItemBeans.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_action_grid_image_details, parent, false);
        return new ActionImageItemHolder(sView);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ActionImageItemHolder imageHolder = (ActionImageItemHolder) holder;
        ActionShowMainItemBean actionItemBean = actionItemBeans.get(position);
        ImageUtils.glideThumbViewByUrl(mContext,imageHolder.chargeThumb,actionItemBean.getCreateUserInfo().getThumb());
        imageHolder.chargeName.setText(actionItemBean.getCreateUserInfo().getName());
        imageHolder.actionName.setText(actionItemBean.getQuestionTitle());
        imageHolder.itemView.post(new Runnable() {
            @Override
            public void run() {
                int width = imageHolder.itemView.getWidth();
                NaizhuAILog.e("width.....=" + width);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageHolder.imageContent.getLayoutParams();
                params.height = width;
                imageHolder.imageContent.setLayoutParams(params);
                ImageUtils.glideImageViewByUrl(mContext,imageHolder.imageContent,actionItemBean.getQuestionImageUrl());
            }
        });

    }


    public class ActionImageItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        CircleImageView chargeThumb;
        TextView chargeName, actionName;
        ImageView imageContent;

        public ActionImageItemHolder(View view) {
            super(view);
            chargeThumb = view.findViewById(R.id.action_charge_thumb);
            chargeName = view.findViewById(R.id.action_charge_user_name);
            actionName = view.findViewById(R.id.action_name);
            imageContent = view.findViewById(R.id.action_item_image_content);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null)
                mItemClickListener.onGridItemClick(v, getAdapterPosition());
        }
    }


    public interface OnItemClickListener {
        void onGridItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
